package top.zhongyy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import top.zhongyy.pojo.Blog;
import top.zhongyy.pojo.Tag;
import top.zhongyy.service.BlogService;
import top.zhongyy.service.BlogTagsService;
import top.zhongyy.service.TagService;

import java.util.List;

/**
 * @Created by IntelliJ IDEA.
 * @DESC:标签前端控制器
 * @Author: Andy
 * @Date: 2021/3/27
 */

@Controller
public class TagShowController {
    
    @Autowired
    private BlogService blogService;
    
    @Autowired
    private BlogTagsService blogTagsService;
    
    @Autowired
    private TagService tagService;
    
    @ApiOperation(value = "跳转到博客标签展示页面")
    @GetMapping("tags/{id}")
    public String tags(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                       @PathVariable("id") Long tagId) {
        List<Tag> tags = tagService.getAllTags();
        if (tagId == -1) {
            tagId = tags.get(0).getId();
        }
        PageHelper.startPage(pageNum, 5, "update_time DESC");
        List<Blog> blogs = blogService.getBlogByTag(tagId);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        model.addAttribute("tags", tags);
        model.addAttribute("page", pageInfo);
        model.addAttribute("activeTagId", tagId);
        return "tags";
    }
}
